package com.anuo.app.modules.coach.controller;

import com.anuo.app.common.globalhandle.BaseController;
import com.anuo.app.common.entity.Request;
import com.anuo.app.common.utils.DateUtil;
import com.anuo.app.common.utils.JsonUtil;
import com.anuo.app.common.utils.Utils;
import com.anuo.app.modules.coach.entity.CoachComment;
import com.anuo.app.modules.coach.service.StudentService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

@RestController
public class InsertCoachDataController extends BaseController {
    @Autowired
    StudentService studentService;


    @PostMapping(value = "v1/apiInsertCoachData", produces = {"application/json;charset=UTF-8"})
    public String apiInsertCoachData(@RequestBody String encryptedReq) {
        //反序列化取得对象
        CoachComment coachCommentReq = JsonUtil.fromJson(encryptedReq,
                new TypeReference<Request<CoachComment>>() {
                }).getFirstEntity();

        int threadNum = Runtime.getRuntime().availableProcessors();
        ExecutorService threadPool = Executors.newFixedThreadPool(threadNum);
        List<Future<String>> taskList = new ArrayList<>();
        //去执行任务
        for (int i = 0; i < 100; i++) {
            Future<String> future = threadPool.submit(new InsertTask(Integer.toString(i)));
            taskList.add(future);
        }
        //获取任务结果
        String result = "";
        for (Future<String> future : taskList) {
            try {
                result = future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.getCause().printStackTrace();
            }
            //打印结果
            System.out.println(result);
        }

        //关闭线程池
        threadPool.shutdown();
        while (!threadPool.isTerminated()) {

        }
        try {
            threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return rspSuccess();
    }



    private class InsertTask implements Callable<String> {
        private String threadName;

        public InsertTask(String threadName) {
            this.threadName = threadName;
        }

        public String getRandomPersonId() {
            List<String> list = new ArrayList<>();
            list.add("3039f5b1481245a99c430124a9627ce4");
            list.add("5539f5b1481245a99c430124a9627c55");
            list.add("6639f5b1481245a99c430124a9627c99");
            int randomIndex = ThreadLocalRandom.current().nextInt(0, 3);
            return list.get(randomIndex);

        }

        public String getRandomComment() {
            List<String> list = new ArrayList<>();
            list.add("感觉还可以");
            list.add("一般般");
            list.add("课程有待改进");
            int randomIndex = ThreadLocalRandom.current().nextInt(0, 3);
            return list.get(randomIndex);

        }


        @Override
        public String call() throws Exception {
            //插入评论
            long begintime = System.nanoTime();
            CoachComment coachCommentInsert = new CoachComment();
            for (int i = 0; i < 10000; i++) {
                coachCommentInsert.preInsert();
                coachCommentInsert.setCoachid(getRandomPersonId());
                coachCommentInsert.setCommentatorid(getRandomPersonId());
                coachCommentInsert.setComment(getRandomComment());
                BigDecimal score = new BigDecimal(ThreadLocalRandom.current().nextInt(0, 6));
                coachCommentInsert.setScore(score);
                coachCommentInsert.setCourseid("6669f5b1481245a99c430124a9627999");
                coachCommentInsert.setShare_name(true);
                System.out.println(threadName + " 执行到 " + i);
                studentService.saveCoachComment(coachCommentInsert);
            }
            long endtime = System.nanoTime();
            long executeTime = DateUtil.nanoTimeToMillisecond(endtime - begintime);
            String msg = "线程" + threadName + "执行时间:" + Long.toString(executeTime);
            return msg;
        }
    }

}
